package cn.hnu.qq.server.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import cn.hnu.qq.entity.ChartContent;
import cn.hnu.qq.entity.Crowd;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.server.dao.ChartContentDao;
@Transactional
public class ChartContentDaoImpl implements ChartContentDao {
	
	@PersistenceContext
	private EntityManager manager;
	
	public void merge(ChartContent content) {
		manager.merge(content);

	}

	public List<ChartContent> getContent(User user, Object object) {
		List<ChartContent> list = null;
		if(object instanceof User){
			User _user = (User) object;
			String jpql = "select c from ChartContent c where c.owner.acount= :acount1 and c.charter.acount = :acount2";
			Query query = manager.createQuery(jpql);
			query.setParameter("acount1", user.getAcount());
			query.setParameter("acount2", _user.getAcount());
			list = query.getResultList();
		}else if(object instanceof Crowd){
			Crowd corwd = (Crowd) object;
			String jpql = "select c from ChartContent c where c.owner.acount= :acount1 and c.crowd.crowdName = :crowdName";
			Query query = manager.createQuery(jpql);
			query.setParameter("acount1", user.getAcount());
			query.setParameter("crowdName", corwd.getCrowdName());
			list = query.getResultList();
		}
		return list;
	}

}
